<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
        <a-form-model-item label="方案编码" prop="id">
          <a-input placeholder="请输入方案编码" :disabled="disableSubmit" v-model.trim="form.id"/>
        </a-form-model-item>
        <a-form-model-item label="方案名称" prop="name">
          <a-input placeholder="请输入方案名称" :disabled="disableSubmit" v-model.trim="form.name"/>
        </a-form-model-item>
        <a-form-model-item label="数据源" prop="entityManagerFactoryName">
          <a-select :disabled="disableSubmit" v-model="form.entityManagerFactoryName" @change="() => this.$set(this.form, 'entityClassName', '')" placeholder="请选择数据源">
            <template v-for="(item,i) in factoryList">
              <a-select-option :key="i" :value="item">{{item}}</a-select-option>
            </template>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="实体类" prop="entityClassName">
          <a-select :disabled="disableSubmit" v-model="form.entityClassName" placeholder="请选择实体类">
            <template v-for="(item,i) in entityManagerFactoryList">
              <a-select-option :key="i" :value="item">{{item}}</a-select-option>
            </template>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="Sheet页名称">
          <a-input :disabled="disableSubmit" placeholder="请输入Sheet页名称" v-model.trim="form.excelSheetName"/>
        </a-form-model-item>
        <a-form-model-item label="起始行">
          <a-input-number style="width:100%;" :disabled="disableSubmit" placeholder="请输入起始行" v-model.trim="form.startRow"/>
        </a-form-model-item>
        <a-form-model-item  label="描述">
          <a-input :disabled="disableSubmit" type="textarea" v-model="form.desc"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { queryFactoryList, queryEntityManagerFactoryList, addExcelImport, editExcelImport } from '@/services/system'

export default {
  name: 'ExcelImportTemplateModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: {},
      validatorRules: {
        name: [{ required: true, message: '请输入方案名称!' }],
        id: [
          { required: true, message: '请输入方案编码!' }
          // { validator: this.validateDictCode }
        ],
        entityManagerFactoryName: [{ required: true, message: '请选择数据源' }],
        entityClassName: [{ required: true, message: '请输入实体类' }]
      },
      disableSubmit: false,
      factoryList: [],
      entityManagerFactoryList: [],
      url: {
        add: '/sys/dict/add',
        edit: '/sys/dict/edit'
      }
    }
  },
  watch: {
    'form.entityManagerFactoryName': {
      handler(val) {
        if (val) {
          this.getEntityManagerFactoryList(val)
        } else {
          this.entityManagerFactoryList = []
        }
      },
      immediate: true

    }
  },
  methods: {
    validateDictCode(rule, value, callback) {
      // 重复校验
      var params = {
        tableName: 'sys_dict',
        fieldName: 'dict_code',
        fieldVal: value,
        dataId: this.model.id
      }
      console.log(params)
      callback()
      // duplicateCheck(params).then((res) => {
      //   if (res.success) {
      //     callback()
      //   } else {
      //     callback(res.message)
      //   }
      // })
    },
    add() {
      this.edit({ itemStatus: 1 })
    },
    edit(record) {
      if (record.id) {
        this.visiblekey = true
      } else {
        this.visiblekey = false
      }
      this.model = Object.assign({}, record)
      this.visible = true
      this.form = pick(this.model, 'id', 'name', 'entityManagerFactoryName', 'entityClassName', 'excelSheetName', 'startRow', 'desc')
    },
    // 确定
    handleOk() {
      // 触发表单验证
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const formData = Object.assign(this.model, this.form)
          if (formData.id) {
            editExcelImport(formData).then(res => {
              this.resSuccess(res)
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            addExcelImport(formData).then(res => {
              this.resSuccess(res)
            }).finally(() => {
              this.confirmLoading = false
            })
          }
        }
      })
    },
    resSuccess(res) {
      if (res.success) {
        this.$message.success(res.message)
        this.$emit('ok')
        this.close()
      } else {
        this.$message.warning(res.message)
      }
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.$refs.ruleForm.resetFields()
      this.visible = false
    },
    getFactoryList() {
      queryFactoryList().then(res => {
        if (res.code === 200) {
          this.factoryList = res.result
        }
      })
    },
    getEntityManagerFactoryList(entityManagerFactoryName) {
      queryEntityManagerFactoryList(entityManagerFactoryName).then(res => {
        if (res.code === 200) {
          this.entityManagerFactoryList = res.result
        }
      })
    }
  },
  mounted() {
    this.getFactoryList()
  }
}
</script>
